//
//  ECUIView_SelectedCategory.m
//  Edu_iOS2018
//
//  Created by yanyan on 2018/1/2.
//  Copyright © 2018年 yanyan. All rights reserved.
//
#import "ECModelData.h"
#import "ECUIView_SelectedCategory.h"
@interface ECUIView_SelectedCategory()<UICollectionViewDelegate,UICollectionViewDataSource>
@property(nonatomic,readonly)UICollectionView *collectionView;
@property(nonatomic,strong)NSIndexPath *selectedIndexPath;
@end
@implementation ECUIView_SelectedCategory
- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = [UIColor colorWithHex:0x000000 alpha:0.4];
       
       UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight * 0.25);
        [btn addTarget:self action:@selector(action_hidden:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:btn];
        UICollectionViewFlowLayout *flowout = [[UICollectionViewFlowLayout alloc]init];
        CGRect cvFrame = CGRectMake(0, kScreenHeight*0.25, frame.size.width, frame.size.height * 0.75);
        flowout.sectionInset = UIEdgeInsetsMake(0,kProportion_Width(45),0,kProportion_Width(45));
        flowout.headerReferenceSize = CGSizeMake(CGRectGetWidth(cvFrame), kProportion_Height(130));
        flowout.footerReferenceSize = CGSizeMake(CGRectGetWidth(cvFrame), kProportion_Height(30));
        
        flowout.minimumLineSpacing = kProportion_Height(36);//每行相隔的距离
        flowout.minimumInteritemSpacing = kProportion_Width(45);//每列相隔的最小距离
        
        _collectionView = [[UICollectionView alloc]initWithFrame:cvFrame collectionViewLayout:flowout];
        //        _collectionView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
        _collectionView.exclusiveTouch = YES;
        _collectionView.allowsMultipleSelection = NO;
        _collectionView.allowsSelection = YES;
        _collectionView.backgroundColor = [UIColor whiteColor];
        _collectionView.scrollEnabled = YES;
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        [_collectionView registerClass:[ECUICVCell_SubCategory class] forCellWithReuseIdentifier:@"cell"];
        [_collectionView registerClass:[ECUICVHeader_Category class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];
        [_collectionView registerClass:[CYUICVHeader_base class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"footer"];
        
        [self addSubview:_collectionView];
        self.layer.masksToBounds = YES;
    }
    return self;
 
}
- (void)action_hidden:(id)sender{
    [self removeFromSuperview];
}

#pragma mark -CollectionView Delegate -
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return self.categorysData.count;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return ((PrimaryCategoryModel *) self.categorysData[section]).subCategoryArray.count;
}
- (CGSize)
collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(floor((CGRectGetWidth(collectionView.frame)-kProportion_Width(46)*4)/3.0) , kProportion_Height(96));
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    PrimaryCategoryModel * subCategorys = self.categorysData[indexPath.section];
    PrimaryCategoryModel *mo = [subCategorys.subCategoryArray objectAtIndex:indexPath.row];
    CYUICVCell_base *cell = (ECUICVCell_SubCategory *)[collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    if (self.selectedCategory) {
        if ([mo.modelId isEqualToString:self.selectedCategory.modelId]) {
            cell.selected = YES;
            _selectedIndexPath = indexPath;
        } else {
            cell.selected = NO;
        }
    }
    cell.object = mo;
    return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSMutableArray * subCategorys = ((PrimaryCategoryModel *)[self.categorysData objectAtIndex:indexPath.section]).subCategoryArray;
    if (_selectedIndexPath) {
        UICollectionViewCell *preSelectedCell = [collectionView cellForItemAtIndexPath:_selectedIndexPath];
        preSelectedCell.selected = NO;
    }
   
    PrimaryCategoryModel *mo_selected = subCategorys[indexPath.row];
    self.selectedCategory = mo_selected;
    UICollectionViewCell *cell = [collectionView cellForItemAtIndexPath:indexPath];
    cell.selected = YES;
    _selectedIndexPath = indexPath;
    
//    for (int i = 0; i < self.categorysData.count; i++) {
//        NSArray *arr = ((PrimaryCategoryModel *)[self.categorysData objectAtIndex:i]).subCategoryArray;
//        for (PrimaryCategoryModel *mo in arr) {
//            if ([mo.modelId isEqualToString:mo_selected.modelId]) {
//                mo_selected.selected = YES;
//            } else {
//            mo.selected = NO;
//            }
//        }
//        }
    if (self.block_selectedAtIndex) {
        self.block_selectedAtIndex(mo_selected, indexPath);
    }
    
}
- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewCell *cell =  [collectionView cellForItemAtIndexPath:indexPath];
    cell.selected = NO;
}
-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    if (kind == UICollectionElementKindSectionHeader) {
        ECUICVHeader_Category *header = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header" forIndexPath:indexPath];
        PrimaryCategoryModel *mo = [self.categorysData objectAtIndex:indexPath.section];
        header.titleLabel.text = mo.title;
        return header;
    }
    CYUICVHeader_base * footer = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"footer" forIndexPath:indexPath];
    UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(kProportion_Width(45), kProportion_Height(30)-1, CGRectGetWidth(collectionView.frame) -  kProportion_Width(90), 0.5)];
    lineView.backgroundColor = [UIColor colorWithHexString:@"#e5e5e5"];
    [footer addSubview:lineView];
    return footer;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
